﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AAAA.cs" company="Rolosoft Ltd">
//   © Rolosoft Ltd
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

#region License

// Copyright 2013 Rolosoft Ltd
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Rsft.Net.Dns.Entities.Records
{
    #region Usings

    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Net;

    using Rsft.Net.Dns.Interfaces;

    #endregion

    /// <summary>
    ///     The AAAA record.
    /// </summary>
    public sealed class AAAA : Record
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AAAA"/> class.
        /// </summary>
        /// <param name="recordReader">
        /// The record reader.
        /// </param>
        public AAAA(IRecordReader recordReader)
            : base(recordReader)
        {
            Contract.Requires(recordReader != null);

            IPAddress myAddress;
            var result =
                IPAddress.TryParse(
                    string.Format(
                        CultureInfo.InvariantCulture, 
                        "{0:x}:{1:x}:{2:x}:{3:x}:{4:x}:{5:x}:{6:x}:{7:x}", 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16(), 
                        recordReader.ReadUInt16()), 
                    out myAddress);

            if (result)
            {
                this.Address = myAddress;
            }
        }

        /// <summary>
        ///     Initializes a new instance of the <see cref="AAAA" /> class.
        /// </summary>
        public AAAA()
        {
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///     Gets Address.
        /// </summary>
        public IPAddress Address { get; private set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///     Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        ///     A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return this.Address.ToString();
        }

        #endregion
    }
}